package com.fnfn.sdk.api.service.impl;

import com.fnfn.sdk.api.common.Constant;
import com.fnfn.sdk.api.common.DateUtil;
import com.fnfn.sdk.api.dao.PermissionDetailMapper;
import com.fnfn.sdk.api.dao.RoleMapper;
import com.fnfn.sdk.api.dao.RolePermissionMapper;
import com.fnfn.sdk.api.entity.PermissionDetail;
import com.fnfn.sdk.api.entity.Role;
import com.fnfn.sdk.api.entity.RolePermission;
import com.fnfn.sdk.api.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("iRoleService")
public class RoleServiceImpl implements IRoleService {

    private final RoleMapper roleMapper;

    private final RolePermissionMapper rolePermissionMapper;

    private final PermissionDetailMapper permissionDetailMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper, RolePermissionMapper rolePermissionMapper,
                           PermissionDetailMapper permissionDetailMapper) {
        this.roleMapper = roleMapper;
        this.rolePermissionMapper = rolePermissionMapper;
        this.permissionDetailMapper = permissionDetailMapper;
    }

    @Override
    public Role selectByRoleName(String roleName) throws Exception {
        return roleMapper.selectByRoleName(roleName);
    }

    @Override
    public void add(Role role) throws Exception {
        role.setCreateTime(DateUtil.formatDateTime(new Date()));
        role.setUpdateTime(DateUtil.formatDateTime(new Date()));
        role.setState(Constant.STATE_EFFECTIVE);
        Role roleName = roleMapper.selectByRoleName(role.getcRoleName());
        if (roleName == null) {
            roleMapper.insertSelective(role);
            System.out.println("=======roleid======" + role.getcRoleId());
        }
        //List<RolePermission> rolePermission = role.getLrolepermission();

        // List<Permission> basePermissions =
        // permissionMapper.selectBasePermission();
        // Set<Integer> permissionIds = role.getcPermissionIds();
        // for (Permission permission : basePermissions) {
        // permissionIds.add(permission.getcPermissionId());
        // }

        // Set<Integer> permissionIds = role.getcPermissionIds();
        Set<Integer> detailIds = role.getcDetailIds();

        // for (Integer permissionId : permissionIds) {

        for (Integer detailId : detailIds) {
            System.out.println("======detailId=========" + detailId);
            PermissionDetail permissionDetailByPrimaryKey = permissionDetailMapper.selectByPrimaryKey(detailId);
            RolePermission rolePermission = new RolePermission();
            rolePermission.setcRoleId(role.getcRoleId());
            //rolePermission.setcPermissionId(permissionDetailByPrimaryKey.getcPermissionId());
            rolePermission.setcPermissionId(detailId);
            rolePermission.setcDetailId(detailId);
            rolePermissionMapper.insertSelective(rolePermission);
        }
    }

    @Override
    public void remove(Integer roleId) throws Exception {
        roleMapper.removeByRoleId(roleId);
    }

    @Override
    public void modify(Role role) throws Exception {
        role.setUpdateTime(DateUtil.formatDateTime(new Date()));
        roleMapper.updateByPrimaryKeySelective(role);

        rolePermissionMapper.deleteBycRoleId(role.getcRoleId());
        Set<Integer> detailIds = role.getcDetailIds();
        RolePermission rolePermission = null;
        if (detailIds != null) {
            for (Integer detailId : detailIds) {
                System.out.println("======detailId=========" + detailId);
                PermissionDetail permissionDetailByPrimaryKey = permissionDetailMapper.selectByPrimaryKey(detailId);
                rolePermission = new RolePermission();
                rolePermission.setcRoleId(role.getcRoleId());
                //rolePermission.setcPermissionId(permissionDetailByPrimaryKey.getcPermissionId());
                rolePermission.setcPermissionId(detailId);
                rolePermission.setcDetailId(detailId);
                rolePermissionMapper.insertSelective(rolePermission);
            }
        }

    }

    @Override
    public List<Role> selectByCondition(Role role) throws Exception {
//        if (StringUtils.isNotEmpty(role.getEndDate())) {
//            role.setEndDate(DateUtil.addDate(role.getEndDate(), 1));
//        }
        return roleMapper.selectByCondition(role);
    }

    @Override
    public List<RolePermission> selectByRoleId(Integer cRoleId) {

        return rolePermissionMapper.selectByRoleId(cRoleId);
    }
}
